package com.hj.controller;

import com.hj.mapper.UserMapper;
import com.hj.pojo.User;
import com.hj.service.UserService;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.util.List;

/**
 * @author Huangjin
 * @version 1.0
 * describe:
 * @date 2023/4/8 9:50
 */
@RestController
//@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public ApiResponse registerUser(@RequestBody User user) {
        if (user == null) {
            return ApiResponse.fail("注册信息为空");
        }

        // 处理注册
        ...

        return ApiResponse.success("注册成功");
    }
   /* @Autowired
    private UserService userService;

    *//**
     * 用户注册
     * @param user 用户数据
     * @return 注册结果
     *//*
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        boolean success = userService.insertUser(User);
        if (success) {
            return new Result(true, "注册成功", null);
        } else {
            return new Result(false, "注册失败", null);
        }
    }*/
    /*//1.加载mybatis的核心配置文件，获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    //2.获取SqlSession对象，用它来执行sql
    SqlSession sqlSession = sqlSessionFactory.openSession();

    public UserController() throws IOException {
    }

    @RequestMapping("/regUser")
    public void register(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("测试注册成功");
        //1.通过request获取表单数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");



        //3.执行sql
        //List<User> users = sqlSession.selectList("test.selectAll");
        //3.1获取UserMapper接口的代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        System.out.println(username);
        User user = userMapper.repeat(username);

        System.out.println(user);

        //4.释放资源
        sqlSession.close();*/

        /*//必要的验证
        if (username == null || username.isEmpty() ||
                password == null || password.isEmpty() ||
                nickname == null || nickname.isEmpty() ||
                agestr == null || agestr.isEmpty() ||
                !agestr.matches("[0-9]+")
        ) {
            response.sendRedirect("/reg_info_error.html");
            return;
        }*/

//    }
    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("测试登录成功");
    }
}
